import { createRouter, createWebHashHistory } from "vue-router";
import Pending_service from "../components/pending_service.vue";
import Record_service from "../components/record_service.vue";
import Details_service from "../components/details_service.vue";
import Application_form_service from "../components/application_form_service.vue";
import ServiceItems from "../components/service_items.vue";
import Items_form from "../components/items_form.vue";
import Login from '../views/Login.vue';
import Main from '../views/Main.vue';
import Register from "../views/Register.vue";

const routes = [
    {
        path: '/',
        name: 'index',
        redirect: '/login'
    },
    {
        path: '/login',
        name: 'Login',
        component: Login
    },
    {
        path: '/register',
        name: 'Register',
        component: Register
    },
    {
        path: '/getProfile',
        name: 'GetProfile',
        component: import('../views/GetProfile.vue'),
    },
    {
        path: '/resetPassword',
        name: 'ResetPassword',
        component: import('../views/ResetPassword.vue'),
    },
    {
        path: '/main',
        name: 'Main',
        component: Main,
        meta: { requiresAuth: true },
        redirect: '/main/dashboard', // 默认重定向到工作台
        children: [
            // 仪表盘
            {
                path: 'dashboard',
                name: 'Dashboard',
                component: () => import('../views/Dashboard.vue')
            },
            // 合同管理
            {
                path: 'contract_wait',
                name: 'contract_wait',
                component: () => import('../components/contract_wait.vue')
            },
            {
                path: 'contract_record',
                name: 'contract_record',
                component: () => import('../components/contract_record.vue')
            },
            {
                path: 'detail/:id',
                name: 'wait_signdetail',
                component: () => import('../components/wait_signdetail.vue'),
                props: true
            },
            {
                path: 'contract/:id',
                name: 'contract_editing',
                component: () => import('../components/contract_editing.vue'),
                props: true
            },
            // 服务管理
            {
                path: 'goToPending',
                name: 'goToPending',
                component: Pending_service
            },
            {
                path: 'goToRecord',
                name: 'goToRecord',
                component: Record_service
            },
            {
                path: 'goToMedicine',
                name: 'goToMedicine',
                component: ServiceItems
            },
            {
                path: 'goToDetails',
                name: 'goToDetails',
                component: Details_service
            },
            {
                path: 'goToApplication',
                name: 'goToApplication',
                component: Application_form_service
            },
            {
                path: 'goToItems',
                name: 'goToItems',
                component: Items_form
            },
            // 住民管理
            {
                path: 'ResidentManagement',
                name: 'ResidentManagement',
                component: () => import('../views/ResidentManagement.vue')
            },
            {
                path: 'ResidentCreate',
                name: 'ResidentCreate',
                component: () => import('../views/ResidentCreate.vue')
            },
            {
                path: 'ResidentInfo',
                name: 'ResidentInfo',
                component: () => import('../views/ResidentInfo.vue')
            },
            {
                path: 'ResidentDetail',
                name: 'ResidentDetail',
                component: () => import('../views/ResidentDetail.vue')
            },
            // 消息管理
            {
                path: 'myMessage',
                name: 'MyMessage',
                component: import('../components/message/MyMessage.vue')
            },
            {
                path: 'myAnnouncement',
                name: 'MyAnnouncement',
                component: import('../components/message/MyAnnouncement.vue')
            },
            {
                path: 'announcement/detail',
                name: 'AnnDetailReply',
                component: import('../components/message/AnnDetailReply.vue')
            },
            {
                path: 'newOrEditAnnouncement',
                name: 'NewOrEditAnnouncement',
                component: import('../components/message/NewOrEditAnnouncement.vue')
            },
            {
                path: 'system/systemAnnouncement',
                name: 'SystemAnnouncement',
                component: () => import('../components/message/SystemAnnouncement.vue')
            },
            // 数据管理
            {
                path: 'InstitutionManagement',
                name: 'InstitutionManagement',
                component: () => import('../views/InstitutionManagement.vue')
            },
            // 系统管理
            {
                path: 'RoleManagement',
                name: 'RoleManagement',
                component: () => import('../views/RoleManagement.vue')
            },
            // 住民地图
            {
                path: 'resident-map',
                name: 'resident_map',
                component: () => import('../components/resident_map.vue')
            },
            // 其他
            {
                path: 'index0',
                name: 'index0',
                component: () => import('../components/index0.vue')
            }
        ]
    }
];

const router = createRouter({
    history: createWebHashHistory(),
    routes,
})

// 路由前置守卫
router.beforeEach((to, from, next) => {
    // 检查是否访问需要认证的路由
    if (to.matched.some(record => record.meta.requiresAuth)) {
        // 检查是否有登录token（这里可以根据你的认证方式调整）
        const token = localStorage.getItem('token') || sessionStorage.getItem('token')
        if (!token) {
            // 没有token，重定向到登录页
            next('/login')
        } else {
            // 有token，允许访问
            next()
        }
    } else {
        // 不需要认证的路由，直接允许访问
        next()
    }
})

export { router }